<html>
<head>
<title>系统记录</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

<link rel="shortcut icon" href="resources/images/lock.png" type="image/x-icon">
<link rel="stylesheet" href="resources/css/stylemain.css">
<link rel="stylesheet" href="resources/layui/css/layui.css">

<link rel="stylesheet" href="resources/layui/css/treeTable.css">
<link rel="stylesheet" href="resources/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<script src="resources/layui/layui.all.js"></script>
<script type="text/javascript" src="resources/js/jquery.js"></script>
<script type="text/javascript" src="resources/js/base64.js"></script>
</head>
<body>

<table style="margin-top:10px">
	<tr>
		<td>
			<button class="layui-btn layui-btn-sm" onclick="exportcap()"><i class="layui-icon layui-icon-export"></i> 导出</button>
		</td>
		<td>
			<label style="margin-left:10px">操作时间:</label><input type="text" id="c_start" placeholder="yyyy-MM-dd" class="table_bar_tool">-<input type="text" id="c_end" placeholder="yyyy-MM-dd" class="table_bar_tool">
		</td>
		<td>
			所属机构:<input type="button" id="f_orgname" name="orgname" onclick="dialog_orgselect()" class="layui-btn layui-btn-primary layui-btn-sm" style="text-align: left;width:200px;margin: 0px 10px;" value="">
		</td>
		<td>
			<button class="layui-btn layui-btn-sm" onclick="doSearch()"><i class="layui-icon layui-icon-search"></i> 查询</button>
		</td>
	</tr>
</table>
<input id="org_id" type="text" name="org_id" style="display:none"></input>

  <table class="layui-hide" id="tbl_log" lay-filter="tbl_log"></table>
  <div id="page"></div>


<script type="text/javascript">
var table = layui.table;
var laypage = layui.laypage;
var laydate = layui.laydate;
var form = layui.form;
var curopt = "";
var _currpage = 1;
var _pageSize = 20;
var start_date = "";
var end_date = "";
var _exportcheck = "";

$("#c_start").blur(function(){
	laydate.render({
	    elem: '#c_start'
	    ,value: myparser($('#c_start').val())
	    ,format: 'yyyy-MM-dd'
	});
});

$("#c_end").blur(function(){
	laydate.render({
	    elem: '#c_end'
	    ,value: myparser($('#c_end').val())
	    ,format: 'yyyy-MM-dd'
	});
});

layui.use([], function(){
	var today = new Date();
	var strYear = today.getFullYear();
	var strDay = pad(today.getDate(), 2);
	var strMonth = pad(today.getMonth(), 2);
    //debugger;
    start_date = (strYear + '-' + strMonth + '-' + strDay);
    end_date =  dateFormat('YYYY-mm-dd',today);

	laydate.render({
	    elem: '#c_start'
	    ,value: start_date
	    ,format: 'yyyy-MM-dd'
	    ,isInitValue: true
	});
    laydate.render({
    	elem: '#c_end'
    	,value: end_date
    	,format: 'yyyy-MM-dd'
    	,isInitValue: true
    });
	getData(_currpage,_pageSize);
	 
});

$('#ckpassword').on('keydown', function (event) {
    if (event.keyCode == 13) {
    	userckopt();
		layer.closeAll();
    }
});

function dialog_orgselect(){
	layer.open({
		  type: 2 
		  ,title: "选择部门"
		  ,skin:"layui-layer-lan" 
		  ,area: ['350px', '400px']
		  ,shade: 0.3
		  ,offset: 'auto'
		  ,anim:5 
		  ,shadeClose: false 
		  ,content: 'getorgtree.html'
		  ,btn: ['取消']	
		  ,success:function (layero,index) {
		        var iframe = window['layui-layer-iframe' + index];              
		        iframe.child("usersyslog_getorg","porg");
		  }
		  ,yes: function(index, layero){ 
			 	layer.close(index);
		  }
		  ,cancel: function(index,layero){ 
		 }
       });
}

function ckdialgo_auth(){
	layer.open({
		  type: 1 
		  ,title: "复核" 
		  ,skin:"layui-layer-lan"
		  ,area: ['350px', '260px'] 
		  ,shade: 0.3
		  ,offset: 'auto'
		  ,anim:5
		  ,shadeClose: false
		  ,content: $("#dlgck")
		  ,btn: ['确定', '取消']
		  ,success:function(layero){
			     //var mask = $(".layui-layer-shade");
			     //mask.appendTo(layero.parent());
			     $('#ckname').focus();
			}
		 ,yes: function(index, layero){

		    	userckopt();
	    		$("#dlgck").css('display','none');
			 	layer.close(index);
		    	
			 	 
		  }
		  ,btn2: function(index, layero){
		    	
		    	$("#dlgck").css('display','none'); 
		    	
		  }
		  ,cancel: function(){ 
			   
			    $("#dlgck").css('display','none'); 
			    
		 }
	});
}
  
function doSearch(){
	start_date = $('#c_start').val();
	end_date = $('#c_end').val();
	_currpage = 1;
	getData(_currpage,_pageSize);
}

function getData(pageNumber,pageSize){

	var datastring = '{"pageNumber":'+pageNumber+',"pageSize":'+ pageSize + ',"start":"' + start_date + '","end":"' + end_date
		+ '","searchval":"","searchproperty":"ipaddr","orgid":"' + $('#org_id').val()+ '","ckname":"","ckpassword":"","rname":"系统管理/日志管理/系统记录"}';
	var layeri = layer.load(2);
	//debugger;
	$.ajax({
		contentType: "application/json;charset=UTF-8",
		url : "usersyslogquery.do?",
		type : "post",
		data : datastring,
		dataType : "json",
		success : function(msg) {
			//debugger;
			_exportcheck = msg.needcheck;
			tablerender(msg);
			page(msg);
			layer.close(layeri);
			if(msg.status != "1"){
				layer.msg(msg.desc);
			}
			
		},
        error : function(e){
        	layer.close(layeri);
        	if(e.status == 200){
        		parent.location.href = "login";
        	}else{
        		alert(e.status);
                alert(e.responseText);
        	}
        }
	});
}

function tablerender(data){	
	table.render({
		  elem: '#tbl_log'
		  //,toolbar: '#toolbarDemo'
		  ,cellMinWidth: 50 
		  ,cols: [[
			 {field:'createtime', title: '操作时间'}
		    ,{field:'createusername', title: '操作人'}
		    ,{field:'descriptor', title: '详细信息'}
		    ,{field:'remote_addr', title: 'IP地址'}
		    ,{field:'rname', title: '功能项'}
		    ,{field:'orgname', title: '所属部门'}
		  ]]
		  ,data:data.datas
		  ,limit: data.pageSize
		  ,height: parent.iframeH - 110
		});		
}

function numpage(pageNumber, pageSize) {
	var datastring = '{"pageNumber":'+pageNumber+',"pageSize":'+ pageSize + ',"start":"' + start_date + '","end":"' + end_date
	+ '","searchval":"","searchproperty":"ipaddr","orgid":"' + $('#org_id').val()+ '","ckname":"","ckpassword":"","rname":"系统管理/日志管理/系统记录"}';
	var layeri = layer.load(2);
	//debugger;
	$.ajax({
		contentType: "application/json;charset=UTF-8",
		url : "usersyslogquery.do?",
		type : "post",
		data : datastring,
		dataType : "json",
		success : function(msg) {
			//debugger;
			tablerender(msg);
			layer.close(layeri);
		},
        error : function(e){
        	layer.close(layeri);
        	if(e.status == 200){
        		parent.location.href = "login";
        	}else{
        		alert(e.status);
                alert(e.responseText);
        	}
        }
	});
}

function page(data) {
    laypage.render({
        elem: 'page', 
        count: data.total, 
        limit: data.pageSize,  
        limits: [10, 20, 30, 40, 50],
        layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
        curr: _currpage,
        jump: function (obj, first) {
            //console.log(obj)

            if (!first) {
                //do something
                _currpage = obj.curr;
                _pageSize = obj.limit;
                numpage(obj.curr, obj.limit)
            }
        }
    });
}

function userckopt(){
	//debugger;
	var ckname = $('#ckname').val();
	if(ckname == ''){
		layer.msg('请填写用户账号!');
		return;
	}
	var ckpassword = $('#ckpassword').val();
	if(_curopt == "export"){
		var datastring = '{"ckname":"' + ckname + '","ckpassword":"' + $.base64.encode(ckpassword) + '","rname":"系统管理/日志管理/日志导出"}';	
		$.ajax({
			contentType: "application/json;charset=UTF-8",
			url : "optcheck.do?",
			type : "post",
			data : datastring,
			dataType : "json",
			success : function(msg) {
				//debugger;
				if(msg.status == "1"){
					$("#dlgck").css('display','none'); 
					layer.closeAll();
					var url = "syslog_download?searchval=&searchproperty=ipaddr&start="+start_date+"&end="+end_date+"&orgid="+$('#org_id').val();
					window.open(url);
				}else{
					layer.msg(msg.desc);
				}
				
			},
	        error : function(e){
	        	if(e.status == 200){
	        		parent.location.href = "login";
	        	}else{
	        		alert(e.status);
	                alert(e.responseText);
	        	}
	        }
		});
	}
	
	
}

function userckopt_cancel(){
	$('#ckname').val('');
	$('#ckpassword').val('');	
}

function exportcap(ckname,ckpassword){
	_curopt = "export";
	if(_exportcheck == "1"){
		$('#ckname').val('');
		$('#ckpassword').val('');
		ckdialgo_auth();
	}else{
		var url = "syslog_download?searchval=&searchproperty=ipaddr&start="+start_date+"&end="+end_date+"&orgid="+$('#org_id').val();
		window.open(url);
	}
	
}

function dateFormat(fmt, date) {
	var ret;
    var opt = {
        "Y+": date.getFullYear().toString(),        
        "m+": (date.getMonth() + 1).toString(),     
        "d+": date.getDate().toString(),           
        "H+": date.getHours().toString(),          
        "M+": date.getMinutes().toString(),        
        "S+": date.getSeconds().toString()         
    };
    for (var k in opt) {
        ret = new RegExp("(" + k + ")").exec(fmt);
        if (ret) {
            fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : pad(opt[k], ret[1].length));
        };
    };
    return fmt;
}

function myparser(s){
	if (!s) {
		return new Date();
	}

	var y,m,d;
	var ss = (s.split('-'));
	if(ss.length > 1){
		y = parseInt(ss[0],10);
		m = parseInt(ss[1],10);
		d = parseInt(ss[2],10);
	}else{
		y = parseInt(ss[0].substring(0,4),10);
		m = parseInt(ss[0].substring(4,6),10);
		d = parseInt(ss[0].substring(6,8),10);
	}
	
	if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
		return new Date(y,m-1,d);
	} else {
		return new Date();
	}
}

function pad(num, n) {  
    var len = num.toString().length;  
    while(len < n) {  
        num = "0" + num;  
        len++;  
    }  
    return num;  
}  

</script>
</body>
</html>
